#include<bits/stdc++.h>
#define N 110
#define int long long
using namespace std;

int n, k, len, tot;
int a[N], b[N];
map<int, int>f;
int use[N], Max[N], Min[N], P[N];
bool ok[N][N];
int now = 0, ans = 1e18;
int lu[N];
void dfs(int X) {
	//cout << X << endl;
	/*for(int i = 1; i <= k; i++) {
		cout << Min[i] << " " << Max[i] << endl;  
	}
	cout << endl;*/
	//cout << now << endl;
	if((double)clock() * 1.0 / CLOCKS_PER_SEC > 1.45) {
		cout << ans;
		exit(0);
	}
	if(now >= ans) return;
	if(X > n) {
		/*for(int i = 1; i <= k; i++) {
			cout << Min[i] << " " << Max[i] << endl;  
		}*/
		/*for(int i = 1; i <= n; i++) cout << lu[i] << " ";
		cout << endl;
		cout << endl;
		cout << now << endl;*/
		if(now < ans) {
			ans = now;
			//cout << ans << endl;
		} 
		return;
	}
	
	int num = 0;
	for(int i = 1; i <= k; i++)
		if(use[i]) num++;
	int h = 0;
	for(int i = 1; i <= tot; i++) h = max(h, P[i]);
	if(num < h) return;
	int x = a[X];
	for(int i = 1; i <= k; i++) {
		if(use[i] == 0 || ok[i][b[X]]) continue;
		//lu[X] = i;
		int lstMax = Max[i], lstMin = Min[i];
		P[b[X]]--;
		use[i]--;
		ok[i][b[X]] = 1;
		Max[i] = max(Max[i], x);
		Min[i] = min(Min[i], x);
		if(use[i] == 0) now += Max[i] - Min[i];
		dfs(X + 1);
		P[b[X]]++;
		if(use[i] == 0) now -= Max[i] - Min[i];
		use[i]++;
		ok[i][b[X]] = 0;
		Max[i] = lstMax;
		Min[i] = lstMin;
	}
}

signed main() {
	freopen("diyiti.in", "r", stdin);
	freopen("diyiti.out", "w", stdout);
	//ios::sync_with_stdio(0);
	//cin.tie(0); cout.tie(0);
	
	cin >> n >> k;
	len = n / k;
	for(int i = 1; i <= n; i++) 
		cin >> a[i];
	
	for(int i = 1; i <= n; i++) {
		f[a[i]]++;
		//cout << f[a[i]] << " ";
		if(f[a[i]] > k) {
			cout << -1;
			return 0;
		}
	}
	
	sort(a + 1, a + n + 1);
	for(int i = 1; i <= n; i++) {
		if(i == 1 || a[i] != a[i - 1]) tot++;
		b[i] = tot;
		P[b[i]]++;
	}

	for(int i = 1; i <= k; i++) use[i] = len, Min[i] = 1e9 + 5;
	dfs(1);
	cout << ans;
	return 0;
}


